/*
参数说明:
alertShow-弹出框参数{
showmsg-服务器弹窗提示
msg-服务器提示内容
}
addressLuckType-领奖界面过来 显示确认领取按钮
addressId-地址Id
recordId-中奖Id
方法：
this.$store.commit(types.STATE_SET_RIGHTCALLBACK, function ()-标头管理callback方法
*/
<template>
  <div>
    <alert v-model="alertShow.showmsg" :title="'提示'" @on-show="onShow" @on-hide="onHide">{{alertShow.msg}}</alert>
    <alert v-model="alertTel.showmsg" :title="'手机绑定'" @on-show="onTelShow" @on-hide="onTelHide">{{alertTel.msg}}</alert>
    <div style="padding-bottom: 28px">
      <x-header style="background-color:#3d82e5;width:100%;position:absolute;left:0;top:0;z-index:100" :left-options="{showBack: true}">
        <p name="content" style="margin-left: -12px">选择收货地址</p>
        <a slot="right" href="javascript:;" style="color: #ffffff" @click="adminressfn" >
          <p name="leftContent">管理</p>
        </a>
      </x-header>
    </div>
    <card :header="{title:''}" v-if="hasAddress===true" v-for="item in userAddList">
      <flexbox  wrap="nowrap" slot="content" @click.native="setDefaultAdd(item)">
        <flexbox-item :span="2" style="text-align: center">
          <icon v-if="item.isDefault===true" type="success"></icon>
        </flexbox-item>
        <flexbox-item :span="10">
          <div style="padding: 5px 0 5px 0;margin-left: -10px">
            <flexbox  wrap="nowrap">
              <flexbox-item :span="6"><h4 class="card-padding">{{item.linkName}}</h4></flexbox-item>
              <flexbox-item :span="6"><h4 class="card-padding">{{item.linkTel}}</h4></flexbox-item>
            </flexbox>
            <flexbox  wrap="nowrap">
              <flexbox-item :span="11">
                <span style="color: #999999">{{item.addressInfo}}</span>
              </flexbox-item>
            </flexbox>
          </div>
        </flexbox-item>
      </flexbox>
    </card>
    <div style="border-bottom: 1px solid #d6d6d6;padding-top: 12px" @click="addFn">
      <img src="../../../../assets/button@2x.png" width="100%;" height="100%" style="display: block"/>
    </div>
    <div style="padding-left: 10px;padding-top:10px;padding-bottom:10px;text-align: left;font-size: 14px;background-color: #ffffff" v-if="addressLuckType===true&&isPointBuy===1">
      <p>手机号码：{{userData.tel}}</p>
    </div>
    <div style="text-align: center;width: 100%;height: 100%;background-color: #ffffff;">
      <div style="padding: 15px;" v-if="addressLuckType===true&&isPointBuy!==1">
        <x-button type="primary" action-type="submit" style="width: 50%;position: relative;background: #3d82e5;font-size: 15px;top: 65%;"  @click.native="gogetShop(1)" >确认领取</x-button>
      </div>
      <group gutter="0" v-if="addressLuckType===true&&isPointBuy===1">
        <x-input title="验证码" class="weui-vcode" placeholder="请输入验证码" v-model="codeData.userCode">
          <x-button v-if="codeData.sendCode===true" slot="right" style="background: #afafaf;position: absolute;right: 6px;top: 10px;padding: 0 3px" type="primary" mini>{{codeData.time}}S后重新发送</x-button>
        </x-input>
      </group>
      <div style="padding: 15px;" v-if="addressLuckType===true&&isPointBuy===1">
        <x-button style="width: 50%;position: relative;background: #3d82e5;font-size: 15px;top: 65%;" @click.native="gogetShop(2)" type="primary">{{buttionName}}</x-button>
      </div>
    </div>
  </div>
</template>
<script>
  import { Flexbox, FlexboxItem, Card, Icon, Alert, XButton, Group, XInput, XHeader } from 'vux'
  import * as types from '../../../../store/types'
  import * as urls from '../../../../api/urls'

  export default {
    components: {
      FlexboxItem,
      Flexbox,
      Card,
      Icon,
      Alert,
      XButton,
      Group,
      XInput,
      XHeader
    },
    mounted: function () {
      window.sessionStorage.setItem('telLuck', '1')
      this.getAddList()
      this.getUserInfodata()
      this.weixinfn40()
      this.recordId = this.$route.query.recordId
      this.addressLuckType = this.$store.state.empower.addressLuckType
      this.isPointBuy = this.$store.state.empower.isPointBuy
      this.itemId = this.$route.query.itemId
    },
    data () {
      return {
        codeData: {
          sendCode: false,
          time: 59,
          userCode: ''
        },
        alertShow: {
          showmsg: false,
          msg: ''
        },
        alertTel: {
          showmsg: false,
          msg: ''
        },
        buttionName: '发送验证码',
//        qqLink: '',
        hasAddress: false,
        userAddList: [],
        addressId: '',
        recordId: '',
        addressLuckType: '',
        isPointBuy: 0,
        itemId: '',
        userData: {
          tel: '',
          balance: ''
        }
      }
    },
    methods: {
      weixinfn40 () {
        let that = this
        let userId = window.sessionStorage.getItem('userId')
        let logo = window.sessionStorage.getItem('LOGO')
        let url = window.location.origin
        let link = url + '/api/wxAuth/index?type=t1' + '&uid=' + userId
        that.$wechat.onMenuShareAppMessage({
          title: '孝行通', // 分享标题
          desc: '孝行通·微商城 一个让子女为父母尽孝的老年服务平台', // 分享描述
          link: link, // 分享链接
          imgUrl: logo, // 分享图标
          success: function () {
            // 用户确认分享后执行的回调函数
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        })
        that.$wechat.onMenuShareTimeline({
          title: '孝行通', // 分享标题
          link: link, // 分享链接
          imgUrl: logo, // 分享图标
          success: function () {
            // 用户确认分享后执行的回调函数
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        })
      },
      getAddList () {
        let userId = window.sessionStorage.getItem('userId')
        let that = this
        that.http(urls.userAddressList, {userId: userId}, function (res) {
          if (res.status === 100) {
            if (res.data.list.length === 0) {
              that.hasAddress = false
            } else {
              that.userAddList = res.data.list
              for (let i = 0; i < that.userAddList.length; i++) {
                that.userAddList[i].addressInfo = that.userAddList[i].pro + that.userAddList[i].city + that.userAddList[i].area + that.userAddList[i].address
                if (that.userAddList[i].isDefault === true) {
                  that.addressId = that.userAddList[i].id
                }
              }
              that.hasAddress = true
            }
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      setDefaultAdd (item) {
        let that = this
        that.http(urls.usersSetDefault, {id: item.id}, function (res) {
          if (res.status === 100) {
            for (let i = 0; i < that.userAddList.length; i++) {
              that.userAddList[i].isDefault = false
            }
            item.isDefault = true
            that.addressId = item.id
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      addFn () {
        this.$router.push({
          path: '/address/AddAddress'
        })
        this.$router.go(1)
      },
      adminressfn () {
        this.$router.push({
          path: '/UserAddress'
        })
        this.$router.go(1)
      },
      gogetShop (type) {
        let that = this
        let userId = window.sessionStorage.getItem('userId')
        if (type === 1) {
          let that = this
          that.http(urls.userIndex, {userId: userId}, function (res) {
            if (res.status === 100) {
              if (res.data.user.tel !== '' && res.data.user.tel !== null && res.data.user.tel !== undefined) {
                that.http(urls.getLottery, {userId: userId, addressId: that.addressId, id: that.recordId}, function (res) {
                  if (res.status === 100) {
                    that.$store.commit(types.USER_ORDER, {index: 2})
                    let params = {
                      isShowHeader: false,
                      isShowTabbar: false
                    }
                    that.$router.push({
                      path: '/order/OrderIndex',
                      query: {
                        params: JSON.stringify(params)
                      }
                    })
                    that.$router.go(1)
                  } else {
                    that.alertShow.showmsg = true
                    that.alertShow.msg = res.errMsg
                  }
                }, function (res) {
                  that.alertShow.showmsg = true
                  that.alertShow.msg = res
                })
              } else {
                that.alertTel.showmsg = true
                that.alertTel.msg = '请先绑定手机号'
              }
            } else {
              that.alertShow.showmsg = true
              that.alertShow.msg = res.errMsg
            }
          }, function (res) {
            that.alertShow.showmsg = true
            that.alertShow.msg = res
          })
        } else {
          that.buttionName = '确认兑换'
          if (that.codeData.userCode !== '') {
            that.http(urls.pointBuy, {userId: userId, addressId: that.addressId, itemId: that.itemId, code: that.codeData.userCode}, function (res) {
              if (res.status === 100) {
                that.$store.commit(types.USER_ORDER, {index: 2})
                let params = {
                  isShowHeader: false,
                  isShowTabbar: false
                }
                that.$router.push({
                  path: '/order/OrderIndex',
                  query: {
                    params: JSON.stringify(params)
                  }
                })
                that.$router.go(1)
              } else {
                that.alertShow.showmsg = true
                that.alertShow.msg = res.errMsg
              }
            }, function (res) {
              that.alertShow.showmsg = true
              that.alertShow.msg = res
            })
          } else {
            if (that.codeData.time === 59) {
              that.codeTelFn()
            } else {
              that.alertShow.showmsg = true
              that.alertShow.msg = '请在' + that.codeData.time + '点击发送新的验证码,如果已收到请填写验证码'
            }
          }
        }
      },
      codeTelFn () {
        let userId = window.sessionStorage.getItem('userId')
        let that = this
        that.http(urls.payMessage, {userId: userId}, function (res) {
          if (res.status === 100) {
            that.alertShow.showmsg = true
            that.alertShow.msg = '验证码已发出，请注意手机查收'
            that.codeData.sendCode = true
            that.setTime()
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      setTime () {
        let interval
        let that = this
        let setTimeFn = function () {
          that.codeData.time--
          if (that.codeData.time < 0) {
            clearInterval(interval)
            that.codeData.sendCode = false
            that.codeData.time = 59
          }
        }
        interval = setInterval(function () {
          setTimeFn()
        }, 1000)
      },
      getUserInfodata () {
        let userId = window.sessionStorage.getItem('userId')
        let that = this
        this.http(urls.userIndex, {userId: userId}, function (res) {
          if (res.status === 100) {
            that.userData.tel = res.data.user.tel
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      onShow () {
        // 弹窗出现时触发
      },
      onHide () {
        // 弹窗关闭时触发
      },
      onTelShow () {
        // 手机未绑定弹窗出现时触发
      },
      onTelHide () {
        // 手机未绑定弹窗关闭时触发
        let that = this
        window.sessionStorage.setItem('telLuck', '0')
        this.$router.push({
          path: '/UserInformation',
          query: {
            recordId: that.recordId
          }
        })
        this.$router.go(1)
      }
    }
  }
</script>
<style scoped="scoped">
</style>
